package dao;

import application.exceptions.ItemNotFoundException;
import mappings.Material;
import mappings.Types;
import mappings.TypesComposition;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import utils.HibernateUtil;

import java.util.List;

public class TypesCompositionDao
{
    private static SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    public List<TypesComposition> getAllTypesCompositionsForMaterial(int materialId)
    {
        Session session = sessionFactory.openSession();

        List<TypesComposition> typesCompositions = session.createCriteria(TypesComposition.class)
                .add(Restrictions.eq("material.id", materialId))
                .list();

        session.close();

        return typesCompositions;
    }

    public TypesComposition addTypesComposition(int materialId, int typeId) throws ItemNotFoundException
    {
        TypesComposition chemicalComposition = new TypesComposition();

        org.hibernate.classic.Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        Material material = (Material) session.get(Material.class, materialId);
        Types type = (Types) session.get(Types.class, typeId);

        if(material == null || type == null)
        {
            tx.rollback();
            session.close();
            throw new ItemNotFoundException();
        }

        chemicalComposition.setMaterial(material);
        chemicalComposition.setTypes(type);

        session.save(chemicalComposition);

        tx.commit();
        session.close();

        return chemicalComposition;
    }

    public TypesComposition editTypesComposition(Integer id, int materialId, int typeId) throws ItemNotFoundException
    {
        org.hibernate.classic.Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        TypesComposition typesCompositionToEdit = (TypesComposition) session.get(TypesComposition.class, id);
        Material material = (Material) session.get(Material.class, materialId);
        Types type = (Types) session.get(Types.class, typeId);

        if(typesCompositionToEdit == null || material == null || type == null)
        {
            tx.rollback();
            session.close();
            throw new ItemNotFoundException();
        }

        typesCompositionToEdit.setMaterial(material);
        typesCompositionToEdit.setTypes(type);
        session.update(typesCompositionToEdit);
        tx.commit();
        session.close();

        return typesCompositionToEdit;
    }

    public void deleteTypesComposition(Integer id) throws ItemNotFoundException
    {
        org.hibernate.classic.Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        int affectedRows = session.createQuery("delete TypesComposition where id = :id")
                .setParameter("id", id)
                .executeUpdate();

        if(affectedRows == 0)
        {
            tx.rollback();
            session.close();
            throw new ItemNotFoundException();
        }

        tx.commit();
        session.close();
    }
}
